package itsm.isperp.framework.solr.parser;

import java.util.List;

import org.ansj.splitWord.analysis.ToAnalysis;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.solr.schema.FieldType;
import org.apache.solr.schema.TextField;
import org.apache.solr.search.QParser;
import org.apache.solr.search.SolrQueryParser;
import org.apache.solr.search.SyntaxError;

/**
 * 把 queryText 转为 and 关系的 queryparser.
 * 
 */
public class ExtSolrQueryParser extends SolrQueryParser {

	public ExtSolrQueryParser(QParser parser, String defaultField) {
		super(parser, defaultField);
	}

	protected Query getFieldQuery(String field, String queryText, boolean quoted)
			throws SyntaxError {
		{
			String myField = field == null ? getDefaultField() : field;

			if (myField != null) {
				FieldType ft = schema.getField(myField).getType();
				if (ft instanceof TextField) {
					List<org.ansj.domain.Term> list = ToAnalysis
							.parse(queryText);
					BooleanQuery bq = new BooleanQuery();// (true);
					for (org.ansj.domain.Term t : list) {
						bq.add(new TermQuery(new Term(myField, t.getName())),
								BooleanClause.Occur.SHOULD);
					}
					return bq;
				}

			}// myField != null

			return super.getFieldQuery(field, queryText, quoted);

		}
	}
}
